package com.iflytek.erp.book.controller;

import com.iflytek.erp.book.service.BookService;
import com.iflytek.erp.util.ReturnUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/book")
public class BookController {


    @Autowired
    private BookService bookService;

    /**
     * 根据书籍id查询书籍基本信息
     */
    @GetMapping("/{id}")
    public Object getById(@PathVariable Integer id) {

        return bookService.getById(id);
    }

    /**
     * 书籍借阅功能
     * * 1、书籍库存减少
     * * 2、读者借书后添加借阅记录
     *
     * @param userId 用户id
     * @param bookId 书籍id
     * @return
     */
    @GetMapping("/borrow")
    public Object borrowBook(Integer userId, Integer bookId) {
        //1.参数判空
        if (null == userId || null == bookId) {
            return ReturnUtil.fail("userId或bookId不能为空");
        }

        int flag = bookService.borrowBook(userId, bookId);
        if (1 == flag) {
            return ReturnUtil.success("书籍借阅成功");
        } else if (0 == flag) {
            return ReturnUtil.fail("该书籍已被借阅完，请过段时间再来借阅");
        } else {
            return ReturnUtil.fail("借阅书籍异常，请稍后重试");
        }
    }

}
